home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3414 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.9 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Nested Structures in C - A Question
  5. Date: 28 Jan 1996 11:44:08 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4egjm8INNnpp@keats.ugrad.cs.ubc.ca>
  8. References: <36400002@peg>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <36400002@peg>,  <tmccoy@peg.apc.org> wrote:
  12. >A Question on Nested Structures in C
  13. >------------------------------------
  14. >
  15. >I've recently been doing some work on the design of the C
  16. >programming language and there seems to be an inconsistency
  17. >in the way nested structures are defined.
  18.  
  19. Your newsreader is adding ^M characters to every line. 
  20.  
  21. >If I want to declare a simple structure called "outer" I
  22. >can do the following:
  23. >
  24. >struct outer {
  25. >    int var1;
  26. >    int var2;
  27. >    int var3;
  28. >};
  29. >
  30. >Apparently the compiler will NOT allocate any storage when
  31. >I do this because, according to Kernighan and Ritchie (2nd
  32. >Ed), "a structure declaration that is not followed by a
  33. >list of variables reserves no storage; it merely describes
  34. >a template or the shape of a structure" (Page 128).
  35.  
  36. Yes, that is right.
  37.  
  38. >This is all well and good.  And when I want to define a new
  39. >structure *within* my old one, I should be able to do:
  40. >
  41. >struct outer {
  42. >    int var1;
  43. >    int var2;
  44. >    int var3;
  45. >    struct inner {
  46. >        int nested1;
  47. >        int nested2;
  48. >    };
  49. >};
  50. >
  51. >and I should be able to define an instance of my structure
  52. >by doing:
  53. >
  54. >struct outer instance;
  55. >
  56. >and refer to the first member of my inner structure by doing:
  57. >
  58. >instance.inner.nested1
  59.  
  60. No, because inner is just a structure label, not an member name.
  61.  
  62. >Yet, C won't let me do this!!  It insists that I define my
  63. >nested structure as follows:
  64. >
  65. >struct outer {
  66. >    int var1;
  67. >    int var2;
  68. >    int var3;
  69. >    struct inner {
  70. >        int nested1;
  71. >        int nested2;
  72. >    } DUMMY;
  73. >};
  74.  
  75. In fact, you can dispense with the word "inner", since the structure "outer"
  76. will act as a template for _all_ the contents.
  77.  
  78. >and after doing
  79. >
  80. >struct outer instance;
  81. >
  82. >I must refer to the first member of my inner structure by
  83. >doing:
  84. >
  85. >instance.DUMMY.nested1
  86. >
  87. >i.e. I must access the inner members via a variable (called
  88. >"DUMMY") instead of being able to use my structure tag
  89. >(i.e. "inner").  In fact, the *only way* I can access the
  90. >inner members is by defining the DUMMY variable within the
  91. >structure template of "outer".
  92.  
  93. Right, you can never use structure tags as variable names (unless you declare a
  94. structure variable with the same name as its tag). Unless a variable or type
  95. called "foo" is declared, the only way the structure label "foo" has a meaning
  96. is when it appears to the right of "struct" in a valid syntactic construct.
  97.  
  98. >I am just wondering whether anybody else has found this to
  99. >be strange and if anybody knows why the nested structure
  100. >feature has been designed in this way.
  101.  
  102. >I'm also curious about how compilers would implement this
  103. >feature.
  104.  
  105. Perhaps you would be more interested in structure inheritance. You declare a
  106. base structure, and then declare another structure as an extension of that one.
  107. Have a look at Oberon.  Oberon lets you derive RECORDs this way, by using the
  108. fields of an existing record as the initial fields of a new record without
  109. nesting.
  110.  
  111. The declaration goes something like:
  112.  
  113. TYPE foo = RECORD 
  114.     X : INTEGER;
  115.     Y : INTEGER;
  116. END;
  117.  
  118. TYPE bar = RECORD (foo) 
  119.     Z : INTEGER;
  120.     W : INTEGER;
  121. END;
  122.  
  123. The record type "bar" has elements X, Y, Z and W all on one "level".
  124. If you find nesting obnoxious, you might like such a feature.  The "foo" in
  125. brackets means to use foo as a base to which the new fields are added.
  126.  
  127. >With a nested structure, as with a simple structure, I
  128. >*should* be able to define a pure structure template in
  129. >which no storage is allocated.
  130.  
  131. Incorrect. A C structure is a collection of variables. A variable is something
  132. that has storage. In essence, a structure is a stamp for allocating storage.
  133. A structure cannot contain a blank template of another structure, because such
  134. a template is not a variable. 
  135.  
  136. It's like in mathematics. Say I have a set of THINGS which I recursively define
  137. to be numbers, or sets of more THINGS. Furthermore I can give it a type name
  138. which helps me mark identical sets of THINGS by the same name. Now, does it
  139. make sense for such a name to also be a member of a set of THINGS? Clearly not,
  140. since it is neither a number, nor a set of THINGS.
  141.  
  142. >  Yet, in the definition I am
  143. >forced to use above, strictly speaking the compiler should
  144. >allocate storage for "DUMMY" even though the enclosing
  145. >structure (i.e. "outer") is only a template.
  146.  
  147. You don't call it "dummy", typically. You normally it a meaningful name.
  148.  
  149. >I find this very inconsistent and it is a feature that bugs
  150. >me about an otherwise elegant language.
  151.  
  152. Nothing inconsistent about it.
  153.  
  154. >Does it bother anybody else?  Or am I just looking at it
  155. >all the wrong way?
  156.  
  157. It's a little bothersome to have to type the extra name to dereference the
  158. inside structure, for lack of inheritance.
  159. -- 
  160.  
  161.